package com.jy.modules.cims.component.balance.servicefee.impl;

import java.math.BigDecimal;

import com.jy.modules.cims.component.balance.servicefee.BaseServiceFee;
import com.jy.modules.cims.data.common.dto.TCMmCostParamDTO;
import com.jy.modules.cims.data.common.dto.TCMmRaisePlanDTO;
import com.jy.modules.cims.data.dto.CreditDetailDto;
import com.jy.modules.cims.data.dto.LoanDetailDto;

public class TBJ extends BaseServiceFee{
	

	final BigDecimal feejs = BigDecimal.valueOf(0.03); //基数0.03
	final BigDecimal yearDay = BigDecimal.valueOf(365); //一年的天数

	/**
	 * 债权-初始化费用计算**************************************************
	 * @author zhangmi
	 * @date 2016-8-16 上午10:49:50
	 * @see com.jy.modules.cims.component.balance.servicefee.ServiceFeeI#doServiceFee(com.jy.modules.cims.data.dto.CreditDetailDto, com.jy.modules.cims.data.common.dto.TCMmCostParamDTO)
	 */
	public BigDecimal doServiceFee(CreditDetailDto creditDetailDto,TCMmCostParamDTO tcmmCostParamDto) {
		//获取服务费基数
		BigDecimal baseAmount = super.getBaseServiceFee(creditDetailDto, tcmmCostParamDto.getServiceFeeBase());
		//获取服务费率
		BigDecimal feeRate = tcmmCostParamDto.getServiceFeeRatio();
		return baseAmount.multiply(feeRate);
	}

	public BigDecimal doGuaranteeFee(CreditDetailDto creditDetailDto,TCMmCostParamDTO tcmmCostParamDto) {
		//获取担保费基数
		BigDecimal baseAmount = super.getBaseBondFee(creditDetailDto, tcmmCostParamDto.getServiceFeeBase());
		//获取担保费率
		BigDecimal bondRate = tcmmCostParamDto.getGuaranteeFeeRatio();
		return baseAmount.multiply(bondRate);
	}

	public BigDecimal doRiskFee(CreditDetailDto creditDetailDto,TCMmCostParamDTO tcmmCostParamDto) {
		//风险金基数
		BigDecimal baseAmount = super.getBaseRiskFee(creditDetailDto, tcmmCostParamDto.getServiceFeeBase());
		//风险金比例
		BigDecimal RiskRate = tcmmCostParamDto.getMarginRatio();
		return baseAmount.multiply(RiskRate);
	}
	

	/**
	 * 贷款-初始化费用计算**************************************************
	 * @author zhangmi
	 * @date 2016-8-16 上午10:50:20
	 * @see com.jy.modules.cims.component.balance.servicefee.ServiceFeeI#doServiceFee(com.jy.modules.cims.data.dto.LoanDetailDto, com.jy.modules.cims.data.common.dto.TCMmCostParamDTO, com.jy.modules.cims.data.common.dto.TCMmRaisePlanDTO)
	 */
	public BigDecimal doServiceFee(LoanDetailDto loanDetailDto,
			TCMmCostParamDTO tcmmCostParamDto, TCMmRaisePlanDTO tcmmRaisePlanDto) {
		
		
		BigDecimal baseAmount = super.getBaseServiceFee(loanDetailDto, tcmmCostParamDto.getServiceFeeBase());
		//获取服务费率
		BigDecimal feeRate = tcmmCostParamDto.getServiceFeeRatio()==null?new BigDecimal(0):tcmmCostParamDto.getServiceFeeRatio();//服务费率
//		BigDecimal period = BigDecimal.valueOf(tcmmRaisePlanDto.getPeriod()==null?0:tcmmRaisePlanDto.getPeriod());//期限
		BigDecimal shPeriod = BigDecimal.valueOf(tcmmRaisePlanDto.getMerchantPeriod()==null?0:tcmmRaisePlanDto.getMerchantPeriod());//商户期限
		BigDecimal packageRate = tcmmCostParamDto.getPackageRatio()==null?new BigDecimal(0):tcmmCostParamDto.getPackageRatio();//打包价
		
		/**
		 * 如果服务费率（服务费率=打包价-利率）小于3%， 
		 * 按照铜板街项目总额*服务费率/365*商户期限
		 * 如果服务费率大于3%，服务费=（小于3%部分服务费+大于3%部分服务费）
		 * 其中小于3%部分服务费=项目总额*3%/365*商户期限
		 * 大于3%部分服务费=项目总额*(（服务费率-3%）/365)*商户期限/（1+（（打包价/365）*商户期限））；
		 */
		if((feeRate.compareTo(feejs)>0)){//大于0.03
			BigDecimal beforeAmount = baseAmount.multiply(feejs).multiply(shPeriod).divide(yearDay,6,BigDecimal.ROUND_HALF_UP);
			BigDecimal afterAmount = baseAmount.multiply(feeRate.subtract(feejs)).multiply(shPeriod).divide(yearDay,6,BigDecimal.ROUND_HALF_UP).divide(packageRate.multiply(shPeriod).divide(yearDay,6,BigDecimal.ROUND_HALF_UP).add(BigDecimal.valueOf(1)),6,BigDecimal.ROUND_HALF_UP);
			return beforeAmount.add(afterAmount).setScale(2, BigDecimal.ROUND_HALF_UP);
		}else{
			//按照铜板街项目总额*服务费率/365*商户期限
			return baseAmount.multiply(feeRate).multiply(shPeriod).divide(yearDay,6,BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
		}
	}

	/**
	 * 担保金
	 */
	public BigDecimal doGuaranteeFee(LoanDetailDto loanDetailDto,
			TCMmCostParamDTO tcmmCostParamDto, TCMmRaisePlanDTO tcmmRaisePlanDto) {
		//获取担保费基数
		BigDecimal baseAmount = super.getBaseBondFee(loanDetailDto, tcmmCostParamDto.getGuaranteeFeeBase());
		//获取担保费率
		BigDecimal bondRate = tcmmCostParamDto.getGuaranteeFeeRatio()==null?new BigDecimal(0):tcmmCostParamDto.getGuaranteeFeeRatio();
		return baseAmount.multiply(bondRate);
	}

	/**
	 * 保证金
	 */
	public BigDecimal doRiskFee(LoanDetailDto loanDetailDto,
			TCMmCostParamDTO tcmmCostParamDto, TCMmRaisePlanDTO tcmmRaisePlanDto) {
		//风险金基数
		BigDecimal baseAmount = super.getBaseRiskFee(loanDetailDto, tcmmCostParamDto.getMarginBase());
		//风险金比例
		BigDecimal RiskRate = tcmmCostParamDto.getMarginRatio()==null?new BigDecimal(0):tcmmCostParamDto.getMarginRatio();
		return baseAmount.multiply(RiskRate);
	}
}
